package Lee_code;

import java.text.DateFormatSymbols;

/**
 * @author blh
 * @date 2024年08月07日 9:03
 */
public class Lee_130_87 {
    int m, n;

    public void solve(char[][] board) {
        m = board.length;
        n = board[0].length;
        for (int i = 0; i < n; i++) {
            if (board[0][i] == '0') {
                dfs(board, 0, i);
            }
            if (board[m][i] == '0') {
                dfs(board, m, i);
            }

        }
        for (int i = 0; i < m; i++) {
            if (board[i][0] == '0') {
                dfs(board, i, 0);
            }
            if (board[i][n] == '0') {
                dfs(board, i, n);
            }
        }

        for (int i = 0; i < board.length; i++) {
            for (int i1 = 0; i1 < board.length; i1++) {
                if (board[i][i1] == 'O') {
                    board[i][i1] = 'X';
                } else if (board[i][i1] == '1') {
                    board[i][i1] = '0';
                }
            }
        }

    }

    private void dfs(char[][] board, int i, int j) {
        // 确保该点有效
        if (i < 0 || i > m - 1 || j < 0 || j > n - 1 || board[i][j] != 0) {
            return;
        } else {
            board[i][j] = 1;
            dfs(board, i + 1, j);
            dfs(board, i - 1, j);
            dfs(board, i, j + 1);
            dfs(board, i, j - 1);
        }
    }
}
